package avltree;

public class AVLTree {
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode parent;
        //平衡因子：右子树高度减左子树高度
        public int bf;
        public TreeNode(int val){
            this.val = val;
        }
    }
    //根节点
    public TreeNode root;
    public boolean insert(int val){
        TreeNode node = new TreeNode(val);
        if(root==null){
            root = node;
        }
        TreeNode parent = null;
        TreeNode cur = root;
        while(cur != null){
            if(val==cur.val){
                return false;
            }else if(cur.val < val){
                parent = cur;
                cur = cur.right;
            }else{
                parent = cur;
                cur = cur.left;
            }
        }
        if(parent.val < val){
            parent.right = node;
        }else{
            parent.left = node;
        }
        node.parent = parent;
        cur = node;
        while(parent != null){
            if(parent.right == cur){
                parent.bf++;
            }else{
                parent.bf--;
            }
            if(parent.bf == 0){
                //已经平衡
                break;
            }else if(parent.bf == -1 || parent.bf == 1){
                //继续循环
                cur = parent;
                parent = cur.parent;
            }else{
                if(parent.bf == 2){
                    if(cur.bf == 1){
                        //左旋
                        rotateLeft(parent);
                    }else{
                        //cur.bf == -1
                        rotateRL(parent);
                    }
                }else{
                    //parent.bf == -2
                    if(cur.bf == -1){
                        rotateRight(parent);
                    }else{
                        //cur.bf == 1
                        rotateLR(parent);
                    }
                }
                break;
            }
        }
        return true;
    }
    private void rotateLeft(TreeNode parent){
        TreeNode subR = parent.right;
        TreeNode subRL = subR.left;
        parent.right = subRL;
        if(subRL != null) {
            subRL.parent = parent;
        }
        subR.left = parent;
        TreeNode pParent = parent.parent;
        parent.parent = subR;
        if(parent == root){
            root = subR;
        }else{
            if(parent == pParent.left){
                pParent.left = subR;
            }else{
                pParent.right = subR;
            }
        }
        subR.parent = pParent;
        parent.bf = 0;
        subR.bf = 0;
    }
    private void rotateRight(TreeNode parent){
        TreeNode subL = parent.left;
        TreeNode subLR = subL.right;
        parent.left = subLR;
        if(subLR != null){
            subLR.parent = parent;
        }
        subL.right = parent;
        TreeNode pParent = parent.parent;
        parent.parent = subL;
        if(parent == root){
            root = subL;
        }else{
            if(parent == pParent.left){
                pParent.left = subL;
            }else{
                pParent.right = subL;
            }
        }
        subL.parent = pParent;
        parent.bf = 0;
        subL.bf = 0;
    }
    private void rotateRL(TreeNode parent){
        TreeNode subR = parent.right;
        TreeNode subRL = subR.left;
        int bf = subRL.bf;
        rotateRight(parent.right);
        rotateLeft(parent);
        if(bf == -1){
            parent.bf = 0;
            subR.bf = -1;
            subRL.bf = 0;
        }else if(bf == 1){
            parent.bf = -1;
            subR.bf = 0;
            subRL.bf = 0;
        }
    }
    private void rotateLR(TreeNode parent){
        TreeNode subL = parent.left;
        TreeNode subLR = subL.right;
        int bf = subLR.bf;
        rotateLeft(parent.left);
        rotateRight(parent);
        if(bf == -1){
            parent.bf = 0;
            subL.bf = -1;
            subLR.bf = 0;
        }else if(bf == 1){
            parent.bf = 0;
            subL.bf = -1;
            subLR.bf = 0;
        }
    }
    private int height(TreeNode root){
        if(root == null){
            return 0;
        }
        int leftH = height(root.left);
        int rightH = height(root.right);
        return leftH>rightH ? leftH+1 : rightH+1;
    }
    public boolean isBalanced(TreeNode root){
        if(root == null){
            return true;
        }
        int leftH = height(root.left);
        int rightH = height(root.right);
        if((rightH-leftH) != root.bf){
            System.out.println("这个节点："+root.val+"平衡因子异常");
            return false;
        }
        return Math.abs(rightH-leftH)<2 && isBalanced(root.left) && isBalanced(root.right);
    }
}
